package com.hbsx.why.utils;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.hbsx.why.utils.refresh.PullRecycleView;

/**
 * @author lb
 * @desc RecyclerView item 样式及滑动事件
 * @time 2017/8/28 15:58
 */

public class RecyclerViewUtils {

    /**
     * 流式布局-网格布局-可滑动
     */
    public static void setStaggeredGridLayout(PullRecycleView pullRecycleView, int spanCount) {
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(spanCount, LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(staggeredGridLayoutManager);
    }

    /**
     * 流式布局-网格布局-是否可滑动
     */
    public static void setScrollStaggeredGridLayout(PullRecycleView pullRecycleView, int spanCount, boolean canScroll) {
        CustomStaggeredGridLayoutManager staggeredGridLayoutManager =
                new CustomStaggeredGridLayoutManager(spanCount, LinearLayoutManager.VERTICAL);
        staggeredGridLayoutManager.setScrollEnabled(canScroll);
        pullRecycleView.setLayoutManager(staggeredGridLayoutManager);
    }

    /**
     * 线性布局--可滑动
     */
    public static void setLinearLayout(PullRecycleView pullRecycleView, Context mContext) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(linearLayoutManager);
    }

    /**
     * 网格布局--可滑动
     */

    public static void setGridLayout(PullRecycleView pullRecycleView, Context mContext, int spanCount) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }
    /**
     * 网格布局--可滑动--横向
     */

    public static void setGridLayoutHorizontal(RecyclerView recycleView, Context mContext, int spanCount) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycleView.setLayoutManager(gridLayoutManager);
    }
    /**
     * 网格布局--是否可滑动
     */
    public static void setScrollGridLayout(PullRecycleView pullRecycleView, Context mContext, int spanCount, boolean canScroll) {
        CustomGridLayoutManager gridLayoutManager = new CustomGridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        gridLayoutManager.setScrollEnabled(canScroll);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }
    /**
     * 网格布局--是否可滑动
     */
    public static void setScrollGridLayout(RecyclerView pullRecycleView, Context mContext, int spanCount, boolean canScroll) {
        CustomGridLayoutManager gridLayoutManager = new CustomGridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        gridLayoutManager.setScrollEnabled(canScroll);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }
    /**
     * @author lb
     * @desc 网格布局--是否可滑动
     * @time 2017/8/28 15:59
     */
    public static class CustomGridLayoutManager extends GridLayoutManager {
        private boolean isScrollEnabled = true;

        public CustomGridLayoutManager(Context context, int spanCount) {
            super(context, spanCount);
        }

        public CustomGridLayoutManager(Context context) {
            super(context, LinearLayoutManager.VERTICAL);
        }

        public void setScrollEnabled(boolean flag) {
            this.isScrollEnabled = flag;
        }

        @Override
        public boolean canScrollVertically() {
            //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
            return isScrollEnabled && super.canScrollVertically();
        }
    }

    /**
     * @author lb
     * @desc 流式布局-网格布局-是否可滑动
     * @time 2017/8/28 15:59
     */
    public static class CustomStaggeredGridLayoutManager extends StaggeredGridLayoutManager {
        private boolean isScrollEnabled = true;

        public CustomStaggeredGridLayoutManager(int spanCount, int orientation) {
            super(spanCount, orientation);
        }


        public void setScrollEnabled(boolean flag) {
            this.isScrollEnabled = flag;
        }

        @Override
        public boolean canScrollVertically() {
            //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
            return isScrollEnabled && super.canScrollVertically();
        }
    }
}
